﻿using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using AMS.DeDuplicator.CommonLayer;

namespace AMS.DeDuplicator.BusinessLayer.Command
{
    public class MoveToTempCommand : Command
    {
        // TODO: Move this to a Constants class so that ClearTempCommand doesn't have to call this
        public const string TempFolder = @"C:\Temp\DeDuplicator Delete";

        private string _path;
        private string _tempPath;  // path of the file in the Temp directory

        #region Constructors
        //public MoveToTempCommand()
        //    : this(null)
        //{
        //}

        public MoveToTempCommand(string path)
            : base()
        {
            InputValidator.ValidateNotNull(path);
            InputValidator.ValidateFileExists(path);
            _path = path;
        }
        #endregion

        public override bool Execute()
        {
            if (this.Executed)
                ExceptionHandler.AlreadyExecutedException();

            InputValidator.ValidateNotNull(_path);
            InputValidator.ValidateFileExists(_path);

            // does temp folder exist yet?  If not, create it
            if (!Directory.Exists(TempFolder))
                Directory.CreateDirectory(TempFolder);
            try
            {
                string fileName = _path.Substring(_path.LastIndexOf("\\") + 1);
                if (File.Exists(String.Format("{0}\\{1}", TempFolder, fileName)))
                {
                    // increment fileName until it doesn't exist
                    // ignore files without types, for now
                    string fileNameHolder = fileName.Substring(0, fileName.LastIndexOf(".") + 1) + '_';
                    string fileType = fileName.Substring(fileName.LastIndexOf("."));
                    int i = 2;
                    while (File.Exists(String.Format("{0}\\{1}{2}{3}", TempFolder, fileNameHolder, i.ToString(), fileType)))
                    { i++; }
                    _tempPath = String.Format("{0}\\{1}{2}{3}", TempFolder, fileNameHolder, i.ToString(), fileType);
                }
                else
                {
                    _tempPath = String.Format("{0}\\{1}", TempFolder, fileName);
                }
                // move file to temp folder
                File.Move(_path, _tempPath);
            }
            catch (Exception)
            {
                this.Executed = !File.Exists(_path);
                return this.Executed;
            }

            this.Executed = true;
            return true;
        }

        //public override bool Execute(object parameter)
        //{
        //    _path = parameter as string;
        //    return this.Execute();
        //}

        public override bool Undo()
        {
            if (!Executed)
                ExceptionHandler.NotExecutedException();

            try
            {
                // move the file from Temp to original place
                File.Move(_tempPath, _path);
            }
            catch (Exception)
            {
                this.Executed = !File.Exists(_path);
                return !this.Executed;
            }

            this.Executed = false;
            return true;
        }
    }
}
